***********************************************************************************************
***
***	INTERTEMPORAL EVIDENCE ON THE STRATEGY OF POPULISM IN THE US (Gennaro, Lecce, Morelli)
***	Replication Package
***  
*** Appendix - Table A5-A6
***
***********************************************************************************************



clear
est clear 

* Indicate location of the replication package here
cd "/Replication Package"

**********************************************
* Upload Data and Merge                  *****
**********************************************

use "3_data/for_analysis/congress_election_both.dta", clear
replace name = trim(name)

preserve

	import delimited "3_data/processed/gpt_populism_scores.csv", colrange(1) numericcols(1) clear 
	drop v1
	
	* fix names match with unicode errors & co
	replace name = "Nydia Velázquez" if name == "Nydia VelÃ¡zquez"
	replace name = `"Jesús "Chuy" García"' if name == `"JesÃºs "Chuy" GarcÃ­a"'
	replace name = "Ann Wagner" if name == "Ann Wegner"
	replace name = "Vicente González" if name == "Vicente GonzÃ¡lez"
	replace name = "Ben Ray Luján" if name == "Ben Ray LujÃ¡n"
	replace name = "Adair Ford Boroughs" if name =="Adair Ford Boroughs -- 2020 -- South Carolina"
	replace name = "Antonio Sabàto Jr." if name == "Antonio SabÃ to Jr."
	replace name = "Andy Briggs" if name == "Andy Biggs"
	replace name = "Charlotte Bergman" if name == "Charlotte Bergmann"
	replace name = "Dean Phillips" if name == "Dean Philips"
	replace name = "G. K. Butterfield" if name == "G.K. Buttefield"
	replace name = "Jackie Walorski" if name == "Jackie Walorwski"
	replace name = "Jackie Walorski" if name == "Jackie Walorwski"
	replace name = "Jeannine Lake" if name == "Jeannine Lee Lake"
	replace name = "Jesús García" if name == "Jesus Garcia"
	replace name = "John Moolenaar" if name == "John Moolenar"
	replace name = "Juan Hidalgo Jr." if name == "Juan Hidalgo"
	replace name = "Kevin McCarthy" if name == "Kevin McCharty"
	replace name = "Madaleine Dean" if name == "Madeleine Dean"
	replace name = "Pramila Jayapal" if name == "Pramila Jaypal"
	replace name = "Raja Krishnamoorthi" if name == "Raja Krishnamoorth"
	replace name = "T.J. Cox" if name == "TJ Cox"
	replace name = "Charlotte Bergmann" if name == "Charlotte Bergman"
	replace name = "Eric Swalwell" if name == "Erik Swalwell"
	replace name = "Chuck Fleischmann" if name == "Charles Fleischmann"
	replace name = "Vicente González" if name == "Vicente Gonzalez"
	replace name = "Gerry Connolly" if name == "Gerald Connolly"
	replace name = "Joe Morelle" if name == "Joseph Morelle"
	replace name = "Mark Green" if name == "Mark Greene"
	replace name = "Mike Doyle" if name == "Michael Doyle"
	replace name = "Mike Simpson" if name == "Michael Simpson"
	replace name = "Mike Turner" if name == "Michael Turner"
	replace name = "Rob Wittman" if name == "Robert Wittmann"
	replace name = "Ron Wright" if name == "Ronald Wright"
	replace name = "Tabitha Johnson-Green" if name == "Tabitha Johnson-Greene"
	replace name = "Terri Sewell" if name == "Terry Sewell"
	replace name = "Tony Cárdenas" if name == "Tony Cardenas"
	replace name = "Tracy Mitrano" if name == "Tracy Miltrano"
	replace name = "Al Lawson" if name == "Alfred Lawson"
	replace name = "Andy Harris" if name == "Andrew Harris"
	replace name = "Cristina Osmeña" if name == "Cristina OsmeÃ±a"
	replace name = "Dan Crenshaw" if name == "Daniel Crenshow"
	replace name = "Tony Cárdenas" if name == "Tony CÃ¡rdenas"

	tempfile temp
	save `temp'
	
restore

replace  name = "Antonio Delgado" if name == "Antonio Delgado_ny"
replace  name = "Antonio Delgado" if name == "Antonio Delgado_CA"

merge 1:1 name state_name election_year using `temp'
tab _m if pop_dim2 !=.
keep if _m!=2
drop _m


**********************************************
* Adjust variables.                      *****
**********************************************


* Controls
egen predicted_s = std(asinh(predicted))


* Outcome variables - standardisation
ds pop_pred*
foreach var in `r(varlist)'{
	
	di "`var'"
	egen `var'_s  = std(`var')

}



**********************************************
* Sample selection.                      *****
**********************************************

keep if uncont==0 & indep==0
global controls "female i.age_cat_num i.ethnicity2 i.educ_cat len_words i.election_year"

**********************************************
* Correlations across outcomes           *****
**********************************************

est clear 

local outvars "pop_predicted_rfw_s pop_predicted_clfw_s"
foreach var in `outvars'{
	
	di "`var'"
	eststo: reg pop_dim2_s `var'
	eststo: areg pop_dim2_s `var', a(state) cluster(district)
	eststo: areg pop_dim2_s `var' $controls, a(state) cluster(district)

	}
	
esttab _all using "4_results/tables/alternative_pop_measures_corr.tex", f label replace booktabs alignment(D{.}{.}{-1}) keep(pop_predicted_rfw_s pop_predicted_clfw_s) s(N r2, fmt(0 2) label("Observations" "R-squared")) nomtitles star(* 0.10 ** 0.05 *** 0.01) r2 b(3) se(3) brac nonotes 

	
**********************************************
* Main table with main outcomes          *****
**********************************************

est clear 

local outvars "pop_predicted_rfw_s pop_predicted_clfw_s"
foreach var in `outvars'{
	
	di "`var'"
	eststo: areg `var' i.outsider_correct $controls, a(state) cluster(district)
	eststo: areg `var' i.outsider_correct##c.econ $controls, a(state) cluster(district)
	eststo: areg `var' i.outsider_correct##i.competitivedistricts##c.econ $controls, a(state) cluster(district)
	eststo: areg `var' i.outsider_correct##i.competitivedistricts##c.econ $controls predicted_s, a(state) cluster(district)

}


esttab _all using "4_results/tables/alternaitve_pop_measures_res.tex", f label replace booktabs alignment(D{.}{.}{-1}) keep(1.outsider_correct 1.competitivedistricts econ 1.outsider_correct#1.competitivedistricts 1.outsider_correct#c.econ 1.competitivedistricts#c.econ  1.outsider_correct#1.competitivedistricts#c.econ) s(N r2, fmt(0 2) label("Observations" "R-squared")) nomtitles star(* 0.10 ** 0.05 *** 0.01) r2 b(3) se(3) brac nonotes 

